package com.youi.util;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams;
import cn.afterturn.easypoi.excel.export.styler.AbstractExcelExportStyler;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;

/**
 * @description: excel导出样式
 * @Author: winty
 * @Date: 2020/4/20 17:43
 */
public class ExcelStyleUtil extends AbstractExcelExportStyler {
    private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT");

    private static final short FONT_SIZE_TEN = 10;

    private static final short FONT_SIZE_ELEVEN = 11;

    private static final short FONT_SIZE_TWELVE = 12;

    /**
     * 标题样式
     */
    private CellStyle headerStyle;

    /**
     * 列表头样式
     */
    private CellStyle titleStyle;

    /**
     * 数据单元格样式
     */
    private CellStyle styles;


    public ExcelStyleUtil(Workbook workbook) {
        this.init(workbook);
    }

    /**
     * 初始化样式
     *
     * @param workbook
     */
    private void init(Workbook workbook) {
        this.headerStyle = initHeaderStyle(workbook);
        this.titleStyle = initTitleStyle(workbook);
        this.styles = initStyles(workbook);
    }


    @Override
    public CellStyle getHeaderStyle(short i) {
        return headerStyle;
    }

    @Override
    public CellStyle getTitleStyle(short i) {
        return titleStyle;
    }

    @Override
    public CellStyle getStyles(boolean b, ExcelExportEntity excelExportEntity) {
        return styles;
    }

    /**
     * 模板使用的样式设置
     *
     * @param isSingle
     * @param excelForEachParams
     * @return
     */
    @Override
    public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) {
        return null;
    }


    private CellStyle initHeaderStyle(Workbook workbook) {
        CellStyle style = setBaseCellStyle(workbook);
        style.setFont(setFontStyle(workbook, FONT_SIZE_TWELVE, true));
        return style;
    }

    private CellStyle initTitleStyle(Workbook workbook) {
        CellStyle style = setBaseCellStyle(workbook);
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        style.setFont(setFontStyle(workbook, FONT_SIZE_TEN, true));
        // 背景色
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
        return style;
    }

    private CellStyle initStyles(Workbook workbook) {
        CellStyle style = setBaseCellStyle(workbook);
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);
        style.setFont(setFontStyle(workbook, FONT_SIZE_TEN, false));
        style.setDataFormat(STRING_FORMAT);
        return style;
    }

    /**
     * 基础样式
     *
     * @return
     */
    private CellStyle setBaseCellStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        // 下边框
        style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        // 左边框
        style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
        // 上边框
        style.setBorderTop(XSSFCellStyle.BORDER_THIN);
        // 右边框
        style.setBorderRight(XSSFCellStyle.BORDER_THIN);
        // 文字水平对齐方式
        style.setAlignment(XSSFCellStyle.ALIGN_LEFT);
        // 文字垂直对齐方式
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        // 设置自动换行
        style.setWrapText(false);
        return style;
    }

    /**
     * 字体样式
     *
     * @param size   字体大小
     * @param isBold 是否加粗
     * @return
     */
    private Font setFontStyle(Workbook workbook, short size, boolean isBold) {
        Font font = workbook.createFont();
        //字体样式
        font.setFontName("Arial");
        //是否加粗
        font.setBold(isBold);
        //字体大小
        font.setFontHeightInPoints(size);
        return font;
    }
}
